//
//  NSDictionary+Exs.swift
//  kxf
//
//  Created by Elavatine on 2025/4/13.
//


extension NSDictionary{
    func stringValueForKey(key:String) -> String {
        let double = self.value(forKey: key)as? Double ?? Double(self.value(forKey: key) as? String ?? "")
        if (double != nil) {
//            DLLog(message: "\(double!)")
//            return "\(double!)"
            return "\(NSDictionary.convertStringToStringThreeDigit("\(double!)") ?? "")"
        }
        return self.value(forKey: key) as? String ?? ""
    }
    func doubleValueForKey(key:String) -> Double {
        var value = self.value(forKey: key)as? String ?? ""
        value = value.replacingOccurrences(of: ",", with: ".")
        if value == ""{
            return self.value(forKey: key) as? Double ?? 0
        }
        return (value as NSString).doubleValue
//        return self.value(forKey: key) as? Double ?? Double(self.value(forKey: key) as? String ?? "0")!
    }
    func floatValueForKey(key:String) -> Float {
        var value = self.value(forKey: key)as? String ?? ""
        value = value.replacingOccurrences(of: ",", with: ".")
        if value == ""{
            return self.value(forKey: key) as? Float ?? 0
        }
        return (value as NSString).floatValue
//        return self.value(forKey: key) as? Double ?? Double(self.value(forKey: key) as? String ?? "0")!
    }
    static func convertStringToStringThreeDigit(_ str: String) -> String? {
        if let doubleValue = Double(str) {
            let numberFormatter = NumberFormatter()
            numberFormatter.minimumFractionDigits = 0
            numberFormatter.maximumFractionDigits = 3
            return numberFormatter.string(from: NSNumber(value: doubleValue))
        }
        return str
    }
}
